home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snipview.zip / VMGRWNT.C < prev   
C/C++ Source or Header  |  1996-07-11  |  13KB  |  519 lines

  1. /*
  2.  *  VMGRWNT.C; VidMgr module for Windows 95/NT compilers.  Release 1.2.
  3.  *
  4.  *  This module written in May 1996 by Andrew Clarke and released to the
  5.  *  public domain.  Last modified in July 1996.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define WIN32_LEAN_AND_MEAN
  12.  
  13. #include <windows.h>
  14. #include "vidmgr.h"
  15.  
  16. #ifdef __CYGWIN32__
  17. typedef WORD far *LPWORD;
  18. #endif
  19.  
  20. static HANDLE HInput = INVALID_HANDLE_VALUE;
  21. static HANDLE HOutput = INVALID_HANDLE_VALUE;
  22. static unsigned long key_hit = 0xFFFFFFFFUL;
  23.  
  24. static int video_init = 0;
  25.  
  26. #define vi_init()  if (!video_init) vm_vi_init()
  27. #define vi_done()  if (video_init) vm_vi_done()
  28.  
  29. void vm_vi_init(void)
  30. {
  31.     HInput = GetStdHandle(STD_INPUT_HANDLE);
  32.     HOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  33.     video_init = 1;
  34. }
  35.  
  36. void vm_vi_done(void)
  37. {
  38.     CloseHandle(HInput);
  39.     HInput = INVALID_HANDLE_VALUE;
  40.     CloseHandle(HOutput);
  41.     HOutput = INVALID_HANDLE_VALUE;
  42.     video_init = 0;
  43. }
  44.  
  45. void vm_init(void)
  46. {
  47.     vi_init();
  48.     vm_getinfo(&vm_startup);
  49.     vm_setattr(vm_startup.attr);
  50. }
  51.  
  52. void vm_done(void)
  53. {
  54.     CONSOLE_CURSOR_INFO cci;
  55.     SetConsoleCursorInfo(HOutput, &cci);
  56.     vm_startup.dwSize = (unsigned short)cci.dwSize;
  57.     vm_startup.bVisible = (int)cci.bVisible;
  58.     vi_done();
  59. }
  60.  
  61. void vm_getinfo(struct vm_info *v)
  62. {
  63.     CONSOLE_CURSOR_INFO cci;
  64.     v->ypos = vm_wherey();
  65.     v->xpos = vm_wherex();
  66.     v->attr = vm_getattrxy(v->xpos, v->ypos);
  67.     v->height = vm_getscreenheight();
  68.     v->width = vm_getscreenwidth();
  69.     cci.dwSize = (DWORD) vm_startup.dwSize;
  70.     cci.bVisible = (BOOL) vm_startup.bVisible;
  71.     GetConsoleCursorInfo(HOutput, &cci);
  72. }
  73.  
  74. char vm_getscreenwidth(void)
  75. {
  76.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  77.     GetConsoleScreenBufferInfo(HOutput, &csbi);
  78.     return (char)csbi.dwSize.X;
  79. }
  80.  
  81. char vm_getscreenheight(void)
  82. {
  83.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  84.     GetConsoleScreenBufferInfo(HOutput, &csbi);
  85.     return (char)csbi.dwSize.Y;
  86. }
  87.  
  88. short vm_getscreensize(void)
  89. {
  90.     return (short)(vm_getscreenwidth() * vm_getscreenheight() * 2);
  91. }
  92.  
  93. char vm_wherex(void)
  94. {
  95.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  96.     GetConsoleScreenBufferInfo(HOutput, &csbi);
  97.     return (char)(csbi.dwCursorPosition.X + 1);
  98. }
  99.  
  100. char vm_wherey(void)
  101. {
  102.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  103.     GetConsoleScreenBufferInfo(HOutput, &csbi);
  104.     return (char)(csbi.dwCursorPosition.Y + 1);
  105. }
  106.  
  107. void vm_gotoxy(char x, char y)
  108. {
  109.     COORD dwCursorPosition;
  110.     dwCursorPosition.X = (SHORT) (x - 1);
  111.     dwCursorPosition.Y = (SHORT) (y - 1);
  112.     SetConsoleCursorPosition(HOutput, dwCursorPosition);
  113. }
  114.  
  115. int vm_kbhit(void)
  116. {
  117.     int iKey = 0;
  118.     INPUT_RECORD irBuffer;
  119.     DWORD pcRead;
  120.  
  121.     if (key_hit != 0xFFFFFFFFUL)
  122.         return (int)key_hit;
  123.  
  124.     memset(&irBuffer, 0, sizeof irBuffer);
  125.  
  126.     if (WaitForSingleObject(HInput, 0L) == 0)
  127.     {
  128.         ReadConsoleInput(HInput, &irBuffer, 1, &pcRead);
  129.         if (irBuffer.EventType == KEY_EVENT &&
  130.           irBuffer.Event.KeyEvent.bKeyDown != 0 &&
  131.           irBuffer.Event.KeyEvent.wRepeatCount <= 1)
  132.         {
  133.             WORD vk, vs, uc;
  134.             BOOL fShift, fAlt, fCtrl;
  135.  
  136.             vk = irBuffer.Event.KeyEvent.wVirtualKeyCode;
  137.             vs = irBuffer.Event.KeyEvent.wVirtualScanCode;
  138. #ifdef __CYGWIN32__
  139.             uc = irBuffer.Event.KeyEvent.AsciiChar;
  140. #else
  141.             uc = irBuffer.Event.KeyEvent.uChar.AsciiChar;
  142. #endif
  143.  
  144.             fShift = (irBuffer.Event.KeyEvent.dwControlKeyState & (SHIFT_PRESSED));
  145.             fAlt = (irBuffer.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED + LEFT_ALT_PRESSED));
  146.             fCtrl = (irBuffer.Event.KeyEvent.dwControlKeyState & (RIGHT_CTRL_PRESSED + LEFT_CTRL_PRESSED));
  147.  
  148.             if (uc == 0)
  149.             {                   /* function keys */
  150.                 switch (vk)
  151.                 {
  152.                 case 0x21:     /* PgUp */
  153.                     if (fCtrl)
  154.                         vs = 0x84;  /* Ctrl+PgUp */
  155.                     break;
  156.                 case 0x22:     /* PgDn */
  157.                     if (fCtrl)
  158.                         vs = 0x76;  /* Ctrl+PgDn */
  159.                     break;
  160.                 case 0x23:     /* End */
  161.                     if (fCtrl)
  162.                         vs = 0x75;  /* Ctrl+End */
  163.                     break;
  164.                 case 0x24:     /* Home */
  165.                     if (fCtrl)
  166.                         vs = 0x77;  /* Ctrl+Home */
  167.                     break;
  168.                 case 0x26:     /* Up Arrow */
  169.                     if (fCtrl)
  170.                         vs = 0x8d;  /* Ctrl+Up Arrow */
  171.                     break;
  172.                 case 0x28:     /* Down Arrow */
  173.                     if (fCtrl)
  174.                         vs = 0x91;  /* Ctrl+Down Arrow */
  175.                     break;
  176.                 case 0x70:     /* F-Keys */
  177.                 case 0x71:
  178.                 case 0x72:
  179.                 case 0x73:
  180.                 case 0x74:
  181.                 case 0x75:
  182.                 case 0x76:
  183.                 case 0x77:
  184.                 case 0x78:
  185.                 case 0x79:
  186.                     if (fAlt)
  187.                         vs += 0x2d;  /* Alt+F-Key */
  188.                     else if (fShift)
  189.                         vs += 0x19;  /* Shift+F-Key */
  190.                     break;
  191.                 }
  192.  
  193.                 if (vk > 0x20 && vk < 0x92)  /* If it's OK use scan code */
  194.                     iKey = (vs << 8);
  195.             }
  196.             else
  197.             {
  198.                 if (fAlt)       /* Alt+Key */
  199.                     iKey = (vs << 8);
  200.                 else if (fCtrl) /* Ctrl+Key */
  201.                     iKey = (vk & 0xbf);
  202.                 else
  203.                     iKey = uc;
  204.             }
  205.         }
  206.     }
  207.  
  208.     if (iKey != 0)
  209.         key_hit = iKey;
  210.     return (int)iKey;
  211. }
  212.  
  213. int vm_getch(void)
  214. {
  215.     int iKey;
  216.     while (key_hit == 0xFFFFFFFFUL)
  217.     {
  218.         vm_kbhit();
  219.     }
  220.     iKey = key_hit;
  221.     key_hit = 0xFFFFFFFFUL;
  222.     return (int)iKey;
  223. }
  224.  
  225. char vm_getchxy(char x, char y)
  226. {
  227.     char ch;
  228.     DWORD len;
  229.     COORD coord;
  230.     coord.X = (DWORD) (x - 1);
  231.     coord.Y = (DWORD) (y - 1);
  232.     ReadConsoleOutputCharacterA(HOutput, &ch, 1, coord, &len);
  233.     return ch;
  234. }
  235.  
  236. char vm_getattrxy(char x, char y)
  237. {
  238.     DWORD len;
  239.     COORD coord;
  240.     WORD wattr;
  241.     coord.X = (DWORD) (x - 1);
  242.     coord.Y = (DWORD) (y - 1);
  243.     ReadConsoleOutputAttribute(HOutput, &wattr, 1, coord, &len);
  244.     return (char)wattr;
  245. }
  246.  
  247. void vm_xgetchxy(char x, char y, char *attr, char *ch)
  248. {
  249.     DWORD len;
  250.     COORD coord;
  251.     WORD wattr;
  252.     coord.X = (DWORD) (x - 1);
  253.     coord.Y = (DWORD) (y - 1);
  254.     ReadConsoleOutputCharacterA(HOutput, ch, 1, coord, &len);
  255.     ReadConsoleOutputAttribute(HOutput, &wattr, 1, coord, &len);
  256.     *attr = (char)wattr;
  257. }
  258.  
  259. void vm_setcursorstyle(int style)
  260. {
  261.     CONSOLE_CURSOR_INFO cci;
  262.     GetConsoleCursorInfo(HOutput, &cci);
  263.     switch (style)
  264.     {
  265.     case CURSORHALF:
  266.         cci.bVisible = 1;
  267.         cci.dwSize = 49;
  268.         SetConsoleCursorInfo(HOutput, &cci);
  269.         break;
  270.     case CURSORFULL:
  271.         cci.bVisible = 1;
  272.         cci.dwSize = 99;
  273.         SetConsoleCursorInfo(HOutput, &cci);
  274.         break;
  275.     case CURSORNORM:
  276.         cci.bVisible = 1;
  277.         cci.dwSize = 12;
  278.         SetConsoleCursorInfo(HOutput, &cci);
  279.         break;
  280.     case CURSORHIDE:
  281.         cci.bVisible = 0;
  282.         SetConsoleCursorInfo(HOutput, &cci);
  283.         break;
  284.     default:
  285.         break;
  286.     }
  287. }
  288.  
  289. void vm_putch(char x, char y, char ch)
  290. {
  291.     DWORD len;
  292.     COORD coord;
  293.     coord.X = (DWORD) (x - 1);
  294.     coord.Y = (DWORD) (y - 1);
  295.     WriteConsoleOutputCharacterA(HOutput, &ch, 1, coord, &len);
  296. }
  297.  
  298. void vm_puts(char x, char y, char *str)
  299. {
  300.     DWORD len;
  301.     COORD coord;
  302.     coord.X = (DWORD) (x - 1);
  303.     coord.Y = (DWORD) (y - 1);
  304.     WriteConsoleOutputCharacterA(HOutput, str, (DWORD) strlen(str), coord, &len);
  305. }
  306.  
  307. void vm_xputch(char x, char y, char attr, char ch)
  308. {
  309.     DWORD len;
  310.     COORD coord;
  311.     WORD wattr;
  312.     coord.X = (DWORD) (x - 1);
  313.     coord.Y = (DWORD) (y - 1);
  314.     wattr = attr;
  315.     WriteConsoleOutputCharacterA(HOutput, &ch, 1, coord, &len);
  316.     WriteConsoleOutputAttribute(HOutput, &wattr, 1, coord, &len);
  317. }
  318.  
  319. void vm_xputs(char x, char y, char attr, char *str)
  320. {
  321.     DWORD i, len;
  322.     COORD coord;
  323.     LPWORD pwattr;
  324.     pwattr = malloc(strlen(str) * sizeof(*pwattr));
  325.     if (!pwattr)
  326.     {
  327.         return;
  328.     }
  329.     coord.X = (DWORD) (x - 1);
  330.     coord.Y = (DWORD) (y - 1);
  331.     for (i = 0; i < strlen(str); i++)
  332.     {
  333.         *(pwattr + i) = attr;
  334.     }
  335.     WriteConsoleOutputCharacterA(HOutput, str, (DWORD) strlen(str), coord, &len);
  336.     WriteConsoleOutputAttribute(HOutput, pwattr, (DWORD) strlen(str), coord, &len);
  337.     free(pwattr);
  338. }
  339.  
  340. void vm_putattr(char x, char y, char attr)
  341. {
  342.     DWORD len;
  343.     COORD coord;
  344.     WORD wattr;
  345.     coord.X = (DWORD) (x - 1);
  346.     coord.Y = (DWORD) (y - 1);
  347.     wattr = attr;
  348.     WriteConsoleOutputAttribute(HOutput, &wattr, 1, coord, &len);
  349. }
  350.  
  351. void vm_paintclearbox(char x1, char y1, char x2, char y2, char attr)
  352. {
  353.     COORD coord;
  354.     LPWORD pwattr;
  355.     char y, *pstr;
  356.     DWORD i, len, width;
  357.     width = (x2 - x1 + 1);
  358.     pwattr = malloc(width * sizeof(*pwattr));
  359.     if (!pwattr)
  360.     {
  361.         return;
  362.     }
  363.     pstr = malloc(width);
  364.     if (!pstr)
  365.     {
  366.         free(pwattr);
  367.         return;
  368.     }
  369.     for (i = 0; i < width; i++)
  370.     {
  371.         *(pwattr + i) = attr;
  372.         *(pstr + i) = ' ';
  373.     }
  374.     for (y = y1; y <= y2; y++)
  375.     {
  376.         coord.X = (DWORD) (x1 - 1);
  377.         coord.Y = (DWORD) (y - 1);
  378.         WriteConsoleOutputCharacterA(HOutput, pstr, width, coord, &len);
  379.         WriteConsoleOutputAttribute(HOutput, pwattr, width, coord, &len);
  380.     }
  381.     free(pwattr);
  382.     free(pstr);
  383. }
  384.  
  385. void vm_paintbox(char x1, char y1, char x2, char y2, char attr)
  386. {
  387.     DWORD i, len, width;
  388.     COORD coord;
  389.     LPWORD pwattr;
  390.     char y;
  391.     width = (x2 - x1 + 1);
  392.     pwattr = malloc(width * sizeof(*pwattr));
  393.     if (!pwattr)
  394.     {
  395.         return;
  396.     }
  397.     for (i = 0; i < width; i++)
  398.     {
  399.         *(pwattr + i) = attr;
  400.     }
  401.     for (y = y1; y <= y2; y++)
  402.     {
  403.         coord.X = (DWORD) (x1 - 1);
  404.         coord.Y = (DWORD) (y - 1);
  405.         WriteConsoleOutputAttribute(HOutput, pwattr, width, coord, &len);
  406.     }
  407.     free(pwattr);
  408. }
  409.  
  410. void vm_clearbox(char x1, char y1, char x2, char y2)
  411. {
  412.     vm_fillbox(x1, y1, x2, y2, ' ');
  413. }
  414.  
  415. void vm_fillbox(char x1, char y1, char x2, char y2, char ch)
  416. {
  417.     DWORD i, len, width;
  418.     COORD coord;
  419.     char y, *pstr;
  420.     width = (x2 - x1 + 1);
  421.     pstr = malloc(width);
  422.     if (!pstr)
  423.     {
  424.         return;
  425.     }
  426.     for (i = 0; i < width; i++)
  427.     {
  428.         *(pstr + i) = ch;
  429.     }
  430.     for (y = y1; y <= y2; y++)
  431.     {
  432.         coord.X = (DWORD) (x1 - 1);
  433.         coord.Y = (DWORD) (y - 1);
  434.         WriteConsoleOutputCharacterA(HOutput, pstr, width, coord, &len);
  435.     }
  436.     free(pstr);
  437. }
  438.  
  439. void vm_gettext(char x1, char y1, char x2, char y2, char *dest)
  440. {
  441.     DWORD i, len, width;
  442.     COORD coord;
  443.     LPWORD pwattr;
  444.     char y, *pstr;
  445.     width = (x2 - x1 + 1);
  446.     pwattr = malloc(width * sizeof(*pwattr));
  447.     if (!pwattr)
  448.     {
  449.         return;
  450.     }
  451.     pstr = malloc(width);
  452.     if (!pstr)
  453.     {
  454.         free(pwattr);
  455.         return;
  456.     }
  457.     for (y = y1; y <= y2; y++)
  458.     {
  459.         coord.X = (DWORD) (x1 - 1);
  460.         coord.Y = (DWORD) (y - 1);
  461.         ReadConsoleOutputCharacterA(HOutput, pstr, width, coord, &len);
  462.         ReadConsoleOutputAttribute(HOutput, pwattr, width, coord, &len);
  463.         for (i = 0; i < width; i++)
  464.         {
  465.             *dest = *(pstr + i);
  466.             dest++;
  467.             *dest = (char)*(pwattr + i);
  468.             dest++;
  469.         }
  470.     }
  471.     free(pwattr);
  472.     free(pstr);
  473. }
  474.  
  475. void vm_puttext(char x1, char y1, char x2, char y2, char *srce)
  476. {
  477.     DWORD i, len, width;
  478.     COORD coord;
  479.     LPWORD pwattr;
  480.     char y, *pstr;
  481.     width = (x2 - x1 + 1);
  482.     pwattr = malloc(width * sizeof(*pwattr));
  483.     if (!pwattr)
  484.     {
  485.         return;
  486.     }
  487.     pstr = malloc(width);
  488.     if (!pstr)
  489.     {
  490.         free(pwattr);
  491.         return;
  492.     }
  493.     for (y = y1; y <= y2; y++)
  494.     {
  495.         for (i = 0; i < width; i++)
  496.         {
  497.             *(pstr + i) = *srce;
  498.             srce++;
  499.             *(pwattr + i) = *srce;
  500.             srce++;
  501.         }
  502.         coord.X = (DWORD) (x1 - 1);
  503.         coord.Y = (DWORD) (y - 1);
  504.         WriteConsoleOutputCharacterA(HOutput, pstr, width, coord, &len);
  505.         WriteConsoleOutputAttribute(HOutput, pwattr, width, coord, &len);
  506.     }
  507.     free(pwattr);
  508.     free(pstr);
  509. }
  510.  
  511. void vm_horizline(char x1, char x2, char row, char attr, char ch)
  512. {
  513.     char x;
  514.     for (x = x1; x <= x2; x++)
  515.     {
  516.         vm_xputch(x, row, attr, ch);
  517.     }
  518. }
  519.